package com.shop.dingdongshoppingapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.dingdongshoppingapi.dao.SearchInfoDao;
import com.shop.dingdongshoppingapi.entity.SearchInfo;
import com.shop.dingdongshoppingapi.service.intf.SearchInfoService;
import com.shop.dingdongshoppingapi.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchInfoServiceImpl implements SearchInfoService {
    @Autowired
    private SearchInfoDao dao;
    /*根据用户查询搜索记录*/
    @Override
    public R select(int token) {
        //入参校验
        if(token>0){
            //查询某一用户的搜索记录，生成条件
            QueryWrapper<SearchInfo> searchInfoQueryWrapper = new QueryWrapper<>();
            searchInfoQueryWrapper.eq("uid",token);
            List<SearchInfo> searchInfos = dao.selectList(searchInfoQueryWrapper);
            //有数据就成功
            if(searchInfos!=null){
                return R.ok(searchInfos);
            }
        }
        return R.fail();
    }

    @Override
    public R selectLast(int token) {
        if(token>0){
            QueryWrapper<SearchInfo> searchInfoQueryWrapper = new QueryWrapper<>();
            searchInfoQueryWrapper.eq("uid",token);
            searchInfoQueryWrapper.orderByDesc("ctime");
            List<SearchInfo> searchInfos = dao.selectList(searchInfoQueryWrapper);
            SearchInfo searchInfo = searchInfos.get(0);
            System.out.println("搜索记录"+searchInfo);
            if(searchInfo!=null){
                return R.ok(searchInfo);
            }
        }
        return R.fail();
    }
}
